export default {
    mounted(el: any) {
        // 处理滚轮事件
        const handleWheel = (e: any) => {
            e.preventDefault();
            el.scrollLeft += e.deltaY;
        };

        // 启用横向滚动
        const enableHorizontalScroll = () => {
            el.addEventListener('wheel', handleWheel, {passive: false});
        };

        // 禁用横向滚动
        const disableHorizontalScroll = () => {
            el.removeEventListener('wheel', handleWheel);
        };

        // 绑定事件
        el.addEventListener('mouseenter', enableHorizontalScroll);
        el.addEventListener('mouseleave', disableHorizontalScroll);

        // 在元素上保存清理函数，以便在指令卸载时使用
        el._hscrollCleanup = () => {
            el.removeEventListener('mouseenter', enableHorizontalScroll);
            el.removeEventListener('mouseleave', disableHorizontalScroll);
            el.removeEventListener('wheel', handleWheel);
        };
    },

    unmounted(el: any) {
        // 清理事件监听器
        if (el._hscrollCleanup) {
            el._hscrollCleanup();
            delete el._hscrollCleanup;
        }
    }
};
